﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Pai.LiveStation.Data;
using Microsoft.Security.Application;
using System.Web.Security;

namespace Pai.LiveStation.Web.Controllers
{
    public class ShowsController : Controller
    {
        //
        // GET: /Show/

        public ViewResult Index()
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                return View(dc.Medias.Where(x => x.MediaType.Name.Equals(MediaType.Show.ToString())).ToList<Media>());
            }
        }

        public ViewResult Create(int id)
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                RadioShow show = dc.RadioShows.Where(x => x.ID == id).FirstOrDefault();
                if(show == null)
                    throw new ArgumentException("Could not find radio show");

                ViewData["RadioShow"] = show;

                return View();
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Media show, FormCollection col,string showAssoc)
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                int id = -1;

                if (!Int32.TryParse(showAssoc, out id))
                    throw new ArgumentException("Invalid radio show");

                RadioShow radioShow = dc.RadioShows.Where(x => x.ID == id).FirstOrDefault();
               
                if(radioShow == null)
                    throw new ArgumentException("Invalid radio show");

                ViewData["RadioShow"] = radioShow;

                show.Name = AntiXss.HtmlEncode(show.Name);
                show.Description = AntiXss.HtmlEncode(show.Description);
                show.MediaType = dc.MediaTypes.Where(x => x.Name.Equals(MediaType.Show.ToString())).FirstOrDefault();

                foreach (var key in col.Keys)
                {
                    //if(key.ToString().StartsWith("Author"))
                    Author author = new Author()
                    {
                    }; 
                }
                
                //Author author = CommonHelper.GetOrCreateAuthorForUser(Membership.GetUser());

                dc.Medias.InsertOnSubmit(show);
                //List<string> tags = new List<string>();
                //tags.AddRange(showTags.Split(';'));
                //tags.ForEach(x =>
                //{
                //    string tagName = x;
                //    Tag tag = dc.Tags.Where(y => y.ID.Equals(tagName)).FirstOrDefault();
                //    if (tag == null)
                //    {
                //        tag.ID = tagName;
                //        dc.Tags.InsertOnSubmit(tag);
                //    }
                //    media.MediaTags.Add(new MediaTag() { Media = media, Tag = tag });
                //});

                string uploadedFile = MediaHelper.UploadFile(Request.Files["showFile"]);

                if (String.IsNullOrEmpty(uploadedFile))
                    ModelState.AddModelError("showFile", "There was a problem uploading your file. Please contact your Executive Producers.");
                else
                    show.Location = uploadedFile;

                if (ModelState.IsValid)
                    dc.SubmitChanges();
                else
                    return View(show);

                return RedirectToAction("Index", "Shows");
            }
        }
        public ActionResult Edit(int id)
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                //int mediaID;

                //if (!Int32.TryParse(id, out mediaID))
                //{
                //    ModelState.AddModelError("Error", "Invalid show id");
                //    return View();
                //}
                
                Media show = dc.Medias.Where(x => x.ID == id)
                    .Where(y => y.MediaType.Equals(MediaType.Show.ToString())).FirstOrDefault();

                if (show == null)
                    throw new ArgumentException("Could not find show");


                return View(show);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Media show)
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                Media updateShow = dc.Medias.Where(x => x.ID == show.ID).Where(y => y.MediaType.Equals(MediaType.Show.ToString())).FirstOrDefault();

                if (updateShow == null)
                    throw new ArgumentException("Invalid show");

                updateShow.Name = AntiXss.HtmlEncode(show.Name);
                updateShow.Description = AntiXss.HtmlEncode(show.Description);

                //List<string> tags = new List<string>();
                //tags.AddRange(showTags.Split(';'));
                //tags.ForEach(x =>
                //{
                //    string tagName = x;
                //    Tag tag = dc.Tags.Where(y => y.ID.Equals(tagName)).FirstOrDefault();
                //    if (tag == null)
                //    {
                //        tag.ID = tagName;
                //        dc.Tags.InsertOnSubmit(tag);
                //    }
                //    media.MediaTags.Add(new MediaTag() { Media = media, Tag = tag });
                //});

                if (ModelState.IsValid)
                    dc.SubmitChanges();


                ViewData["Show"] = show;

                return View();
            }
        }
        public ActionResult Delete(int id)
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                //int mediaID;
                //Media media = new Media();

                //if (!Int32.TryParse(id, out mediaID))
                //{
                //    ModelState.AddModelError("Error", "Invalid show id");
                //    return RedirectToAction("Index");
                //}

                Media show = dc.Medias.Where(x => x.ID == id).FirstOrDefault();

                if (show == null)
                    throw new ArgumentException("Invalid show");

                dc.Medias.DeleteOnSubmit(show);
                dc.SubmitChanges();

                return RedirectToAction("Index");
            }
        }

    }
}
